<?php

class NCore_Page_Debugger
{
	public function getDebugInfo($ports)
	{
		$markup = <<<EOD
<style media="all" type="text/css">
/* Debug styles */
div#debug {
margin-top: 30px;
border: 1px solid #000;
font-family: monospace;
background-color: #ff9;
padding: 0px 10px 10px 10px;
color:#000;
}
div#debug h1 {
background-color: #000;
color: #fff;
margin: 0px -10px;
padding: 10px;
}
div#debug h2 {
margin-top:20px;
}
div#debug table {border-color: #000;}
div#debug th {background: #000;color: #fff;}
div#debug th, div#debug td {border-color: #000; padding:5px;}
</style>
EOD;
        $markup.= '<div id="debug">';
		$markup.= '<h1>Debug info</h1>';
		$markup.= '<h2>Ports</h2>';
		$markup.= '<table><tr><th>Name</th><th>Value</th><th>Valid</th></tr>';
		foreach($ports as $port)
		{
            if($port instanceof NCore_CompositePort)
                continue;
                
			try
			{
				$isValid = ($port->isValid())?'Yes':'No';
			}
			catch(Exception $e)
			{
				$isValid = 'Not validated';
			}
			$ga = $port->getGlobalArray();
			$markup.= '<tr><td>'.$port->getName().'</td><td>'.((isset($ga[$port->getGlobalArrayKey()]))?$ga[$port->getGlobalArrayKey()]:'Not set').'</td><td>'.$isValid.'</td></tr>';
		}
		$markup.= '</table>';
		$markup.= '<h2>Get vars</h2>';
		$markup.= '<table><tr><th>Name</th><th>Value</th></tr>';
		foreach($_GET as $key=>$value)
		{
            $markup.= $this->createVariableOuptut($key, $value);
		}
		$markup.= '</table>';
		$markup.= '<h2>Post vars</h2>';
		$markup.= '<table><tr><th>Name</th><th>Value</th></tr>';
		foreach($_POST as $key=>$value)
		{
			$markup.= $this->createVariableOuptut($key, $value);
		}
		$markup.= '</table>';
		$markup.= '<h2>Cookie vars</h2>';
		$markup.= '<table><tr><th>Name</th><th>Value</th></tr>';
		foreach($_COOKIE as $key=>$value)
		{
			$markup.= $this->createVariableOuptut($key, $value);
		}
		$markup.= '</table>';
		$markup.= '<h2>Session vars</h2>';
		$markup.= '<table><tr><th>Name</th><th>Value</th></tr>';
		foreach($_SESSION as $key=>$value)
		{
			$markup.= $this->createVariableOuptut($key, $value);
		}
		$markup.= '</table>';
		$markup.= '<!--Thanks to Davin Granroth (davingranroth.com) for some CSS contributions to the debug output.--></div>';
		return $markup;	
	}

    public function createVariableOuptut($key, $value)
    {
        if (is_array($value))
        {
            if (count($value) < 1)
            {
                return '<tr><td>'.$key.'</td><td><em>EMPTY ARRAY</em></td></tr>';
            }
            else
            {
                return '<tr><td>'.$key.'</td><td>'.(implode(',',$value)).'</td></tr>';
            }
        }
        else
        {
            return '<tr><td>'.$key.'</td><td>'.$value.'</td></tr>';
        }
    }
}

?>